package cz.acies.genea.data.gedcom.tools;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.xml.sax.SAXParseException;

import cz.acies.genea.data.gedcom.model.Gedcom;
import cz.acies.genea.data.gedcom.parser.ModelParser;
import cz.acies.genea.data.gedcom.visitors.GedcomWriter;

import java.io.*;
import java.util.logging.Logger;

public class Gedcom2Gedcom
{
	private static final Logger logger = Logger.getLogger("org.folg.gedcom.tools");

	@Option(name = "-i", required = true, usage = "file or directory containing gedcom files to convert")
	private File gedcomIn;

	@Option(name = "-o", required = false, usage = "target directory")
	private File gedcomOut;

	private ModelParser parser;
	private GedcomWriter writer;

	public Gedcom2Gedcom() {
		parser = new ModelParser();
		writer = new GedcomWriter();
	}

	public void convertGedcom(File file) {
		try {
			Gedcom gedcom = parser.parseGedcom(file);
			OutputStream out = (gedcomOut != null ? new FileOutputStream(new File(gedcomOut, file.getName())) : new ByteArrayOutputStream());
			writer.write(gedcom, out);
			if (gedcomOut != null) {
				out.close();
			} else {
				System.out.println(out.toString());
			}
		} catch (SAXParseException e) {
			logger.severe("SaxParseException for file: " + file.getName() + " " + e.getMessage() + " @ " + e.getLineNumber());
		} catch (IOException e) {
			logger.severe("IOException for file: " + file.getName() + " " + e.getMessage());
		} catch (RuntimeException e) {
			e.printStackTrace();
			logger.severe("Exception for file: " + file.getName() + " " + e.getMessage());
		}
	}

	private void doMain() throws FileNotFoundException {
		if (gedcomIn.isDirectory()) {
			for (File file : gedcomIn.listFiles()) {
				convertGedcom(file);
			}
		} else if (gedcomIn.isFile()) {
			convertGedcom(gedcomIn);
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		Gedcom2Gedcom self = new Gedcom2Gedcom();
		CmdLineParser parser = new CmdLineParser(self);
		try {
			parser.parseArgument(args);
			self.doMain();
		} catch (CmdLineException e) {
			System.err.println(e.getMessage());
			parser.printUsage(System.err);
		}
	}
}
